﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitiesDAL
{
    public class DuocTraDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Duoctra");
        }

        //public DataTable Select_Tungphieumuon()
        //{
        //    return ExecuteQuery("sp_Select_Tungphieumuon");
        //}

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Duoctra {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (DuocTra)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Duoctra '{0}', '{1}', '{2}', N'{3}' ";
            sql = string.Format(sql, o.STT_PM, o.MACUONSACH, o.NGAYTRA.ToString("dd/MM/yyyy"), o.TINHTRANGSACH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (DuocTra)obj;
            var sql = "set dateformat dmy; execute sp_Update_Duoctra '{0}', '{1}', '{2}', N'{3}' ";
            sql = string.Format(sql, o.STT_PM, o.MACUONSACH, o.NGAYTRA.ToString("dd/MM/yyyy"), o.TINHTRANGSACH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select c.STT_PM, b.MACUONSACH, e.TENDAUSACH, f.TENTHELOAI, i.TENNHAXB, LTRIM(rtrim(d.HODOCGIA)) + ' ' + LTRIM(rtrim(d.TENDOCGIA)) as HOVATEN, c.NGAYMUON, a.NGAYTRA, a.TINHTRANGSACH
                        from tb_Duoctra a join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH join tb_Phieumuon c on a.STT_PM=c.STT_PM join tb_Thedocgia d on c.MADOCGIA=d.MADOCGIA join tb_Dausach e on b.MADAUSACH=e.MADAUSACH join tb_Theloai f on e.MATHELOAI=f.MATHELOAI join tb_Nhaxuatban i on e.MANHAXB=i.MANHAXB";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.DuocTra o)
        {
            string sql;
            if (o.STT_PM != "")
                sql = @"Select c.STT_PM, b.MACUONSACH, e.TENDAUSACH, f.TENTHELOAI, i.TENNHAXB, LTRIM(rtrim(d.HODOCGIA)) + ' ' + LTRIM(rtrim(d.TENDOCGIA)) as HOVATEN, c.NGAYMUON, a.NGAYTRA, a.TINHTRANGSACH
                        from tb_Duoctra a join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH join tb_Phieumuon c on a.STT_PM=c.STT_PM join tb_Thedocgia d on c.MADOCGIA=d.MADOCGIA join tb_Dausach e on b.MADAUSACH=e.MADAUSACH join tb_Theloai f on e.MATHELOAI=f.MATHELOAI join tb_Nhaxuatban i on e.MANHAXB=i.MANHAXB Where c.STT_PM like '%" + o.STT_PM + "%'  ";
            else
                sql = @"Select c.STT_PM, b.MACUONSACH, e.TENDAUSACH, f.TENTHELOAI, i.TENNHAXB, LTRIM(rtrim(d.HODOCGIA)) + ' ' + LTRIM(rtrim(d.TENDOCGIA)) as HOVATEN, c.NGAYMUON, a.NGAYTRA, a.TINHTRANGSACH
                        from tb_Duoctra a join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH join tb_Phieumuon c on a.STT_PM=c.STT_PM join tb_Thedocgia d on c.MADOCGIA=d.MADOCGIA join tb_Dausach e on b.MADAUSACH=e.MADAUSACH join tb_Theloai f on e.MATHELOAI=f.MATHELOAI join tb_Nhaxuatban i on e.MANHAXB=i.MANHAXB Where b.MACUONSACH like '%" + o.MACUONSACH + "%' or a.NGAYTRA like '%" + o.NGAYTRA + "%' or a.TINHTRANGSACH like '%" + o.TINHTRANGSACH + "%'  ";
            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(STT_PM) as Ma From tb_Duoctra";
            return ExecuteQuery(sql);
        }


        #endregion
    }
}
